var Plane = function () {
    this.blood = 100;
    this.attackLevel = 1;
    this.defenseLevel = 1;
};

var plane = new Plane();
plane.blood = 500;
plane.attackLevel = 10;
plane.defenseLevel = 7;

var clonePlane = Object.create(plane);

console.log(plane);
console.log(clonePlane);

//原型编程范型
//所有数据都是对象
//要得到一个对象，不是通过实例化类，而是找到一个对象作为原型并克隆它
//对象会记住他的原型
//如果对象无法响应某个请求，它会把这个请求委托给自己的原型


//javascript中的根对象是Object.prototype对象

var obj1 = new  Object();
var obj2 = {};

console.log(Object.getPrototypeOf(obj1) === Object.prototype);
console.log(Object.getPrototypeOf(obj2) === Object.prototype);

//使用new 运算符从构造器中得到一个对象

function  Person(name) {
    this.name = name;
}

Person.prototype.getName = function(){
    return this.name;
}

var a = new Person('sven');

console.log(a.name);
console.log(a.getName());

console.log(Object.getPrototypeOf(a) === Person.prototype);



//new 运算的过程
function Person(name) {
    this.name = name;
}

Person.prototype.getName = function(){
    return this.name;
}

var objectFactory = function () {
    var obj = new Object();//从Object上克隆一个空对象
    var Constructor = [].shift.call(arguments);//取得外部传入的构造器
    obj.__proto__ = Constructor.prototype;//指向正确的类型  原先指向 Object.prototype
    var ret = Constructor.apply(obj,arguments);//借用外部传入的构造器给obj设置属性
    return typeof ret === 'object' ? ret : obj;
}


var a = objectFactory(Person,'sevn');

console.log(a.name);
console.log(a.getName());

console.log(Object.getPrototypeOf(a) === Person.prototype);

//对象会记住他的原型
var a = new Object();

console.log(a.__proto__===Object.prototype);


//原型继承方式
//先从a自身里找name属性，但是没有，然后去a的原型obj里找name属性
var obj = {name:'wqy'};

var A = function () {};
A.prototype = obj; //把A的原型指向obj

var a = new A();
console.log(a.name);

//obj的原型还是Object.prototype对象
console.log(Object.getPrototypeOf(obj)===Object.prototype);

//一个 "类" 继承自另外一个 "类"
//b->B.prototype(A)->A.prototype
var A = function () {};
A.prototype = {
    name:'zdp'
};

var B = function () {};
B.prototype = new A();

var b = new B();
console.log(b.name);
